package com.bsoft.gol.hcs.specialist.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import com.bsoft.gol.hcs.specialist.entity.FusHealthEducationEntity;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.List;

/**
 * 健康宣教DAO
 *
 * @author niule
 **/
public abstract class FusHealthEducationDAO extends HibernateSupportDelegateDAO<FusHealthEducationEntity> {

    @DAOMethod(sql = " from FusHealthEducationEntity where fusHealthEducationId =:id and type=:type order by sort asc ")
    public abstract List<FusHealthEducationEntity> findFusHealthEducationListByIdAndType(@DAOParam("id") Long id,
                                                                                         @DAOParam("type") Integer type);

    @DAOMethod(sql = " from FusHealthEducationEntity where fusHealthEducationId =:id")
    public abstract List<FusHealthEducationEntity> findFusHealthEducationSubListByIdAndType(@DAOParam("id") Long id);

    @DAOMethod(sql = " from FusHealthEducationEntity where fusHealthEducationId in (:educationIdList) ")
    public abstract List<FusHealthEducationEntity> findFusHealthEducationSubListByIdList(@DAOParam("educationIdList") List<Long> educationIdList);

    @DAOMethod(sql = " from FusHealthEducationEntity where departmentId =:departmentId  order by sort asc ")
    public abstract List<FusHealthEducationEntity> findFusHealthEducationSubListByDepartmentId(@DAOParam(
            "departmentId") String departmentId);

    @DAOMethod(sql = " from FusHealthEducationEntity where org_id =:orgId  order by sort asc ")
    public abstract List<FusHealthEducationEntity> findFusHealthEducationSubListByOrgId(@DAOParam("orgId") String orgId);


    @DAOMethod(sql = " from FusHealthEducationEntity where orgId =:orgId and  departmentId =:departmentId   order by " +
            "sort asc ")
    public abstract List<FusHealthEducationEntity> findListByOrgIdAndDepartmentId(@DAOParam("orgId") String orgId,
                                                                                  @DAOParam("departmentId") String departmentId);


    @DAOMethod(sql = " from FusHealthEducationEntity where parentId is null and type=1 order by sort asc ")
    public abstract List<FusHealthEducationEntity> findFusHealthEducationTopCatalog();

    @DAOMethod(sql = " from FusHealthEducationEntity where parentId=:fusHealthEducationId order by sort asc ")
    public abstract List<FusHealthEducationEntity> findLowerFusHealthEducationList(@DAOParam("fusHealthEducationId") Long fusHealthEducationId);

    @DAOMethod(sql = " delete from FusHealthEducationEntity where fusHealthEducationId=:fusHealthEducationId ")
    public abstract void deleteFusHealthEducationById(@DAOParam("fusHealthEducationId") Long fusHealthEducationId);

    @DAOMethod(sql = " from FusHealthEducationEntity where fusHealthEducationId =:fusHealthEducationId and type=2 ")
    public abstract FusHealthEducationEntity getFusHealthEducationContent(@DAOParam("fusHealthEducationId") Long fusHealthEducationId);

    @DAOMethod(sql = " update FusHealthEducationEntity set sort=:sort where fusHealthEducationId =:fusHealthEducationId and type=1 ")
    public abstract void updateCurrentCatalogSortById(@DAOParam("fusHealthEducationId") Long fusHealthEducationId, @DAOParam("sort") Integer sort);

    @DAOMethod(sql = " from FusHealthEducationEntity where name=:name and type=2 ")
    public abstract FusHealthEducationEntity getFusHealthEducationByName(@DAOParam("name") String name);

    public void updateFusHealthEducationById(final FusHealthEducationEntity fusHealthEducation) {
        HibernateStatelessResultAction<Object> action = new AbstractHibernateStatelessResultAction<Object>() {
            @Override
            public void execute(StatelessSession statelessSession) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("update FusHealthEducationEntity set name=:name");

                if (CommonUtils.isNotEmpty(fusHealthEducation.getContent())) {
                    sql.append(" ,content=:content ");
                }
                if (CommonUtils.isNotEmpty(fusHealthEducation.getSourceType())) {
                    sql.append(" ,sourceType=:sourceType ");
                }
                if (CommonUtils.isNotEmpty(fusHealthEducation.getDefaultPush())) {
                    sql.append(" ,defaultPush=:defaultPush ");
                }
                sql.append("  where fusHealthEducationId=:fusHealthEducationId ");

                Query squery = statelessSession.createQuery(sql.toString());

                squery.setParameter("name", fusHealthEducation.getName());
                squery.setParameter("fusHealthEducationId", fusHealthEducation.getFusHealthEducationId());

                if (StringUtils.isNotEmpty(fusHealthEducation.getContent())) {
                    squery.setParameter("content", fusHealthEducation.getContent());
                }
                if (CommonUtils.isNotEmpty(fusHealthEducation.getSourceType())) {
                    squery.setParameter("sourceType", fusHealthEducation.getSourceType());
                }
                if (CommonUtils.isNotEmpty(fusHealthEducation.getDefaultPush())) {
                    squery.setParameter("defaultPush", fusHealthEducation.getDefaultPush());
                }
                squery.executeUpdate();
            }
        };
        HibernateSessionTemplate.instance().execute(action);
    }

    public void updateNextCatalogSort(final Integer sort, final Long parentId) {
        HibernateStatelessResultAction<Object> action = new AbstractHibernateStatelessResultAction<Object>() {
            @Override
            public void execute(StatelessSession statelessSession) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("update FusHealthEducationEntity set sort=sort-1 where type=1 and sort=:sort ");
                if (parentId != null) {
                    sql.append(" and parentId=:parentId ");
                } else {
                    sql.append(" and parentId is null ");
                }
                Query squery = statelessSession.createQuery(sql.toString());
                squery.setParameter("sort", sort);
                if (parentId != null) {
                    squery.setParameter("parentId", parentId);
                }
                squery.executeUpdate();
            }
        };
        HibernateSessionTemplate.instance().execute(action);
    }

    public void updateLastCatalogSort(final Integer sort, final Long parentId) {
        HibernateStatelessResultAction<Object> action = new AbstractHibernateStatelessResultAction<Object>() {
            @Override
            public void execute(StatelessSession statelessSession) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("update FusHealthEducationEntity set sort=sort+1 where type=1 and sort=:sort ");
                if (parentId != null) {
                    sql.append(" and parentId=:parentId ");
                } else {
                    sql.append(" and parentId is null ");
                }
                Query squery = statelessSession.createQuery(sql.toString());
                squery.setParameter("sort", sort);
                if (parentId != null) {
                    squery.setParameter("parentId", parentId);
                }
                squery.executeUpdate();
            }
        };
        HibernateSessionTemplate.instance().execute(action);
    }

    public void updateCatalogSort(final Integer sort, final Long parentId) {
        HibernateStatelessResultAction<Object> action = new AbstractHibernateStatelessResultAction<Object>() {
            @Override
            public void execute(StatelessSession statelessSession) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("update FusHealthEducationEntity set sort=sort-1 where type=1 and sort>:sort ");
                if (parentId != null) {
                    sql.append(" and parentId=:parentId ");
                } else {
                    sql.append(" and parentId is null ");
                }
                Query squery = statelessSession.createQuery(sql.toString());
                squery.setParameter("sort", sort);
                if (parentId != null) {
                    squery.setParameter("parentId", parentId);
                }
                squery.executeUpdate();
            }
        };
        HibernateSessionTemplate.instance().execute(action);
    }

    @DAOMethod(sql = " from FusHealthEducationEntity  order by sort asc ")
    public abstract List<FusHealthEducationEntity> findFusHealthEducationSubAllList();
}
